﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors.Exchange
{
    public partial class purchaseDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData(Convert.ToInt32(Request.QueryString["pid"]));

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if(!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the data of a purchase, based on the input parameter.
        /// </summary>
        /// <param name="purchaseId">Specifies the ID of the sale to be bound.</param>
        private void BindData(int purchaseId)
        {
            Purchase pur = dbExchange.GetPurchaseDetails(purchaseId);

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            lblPurchaseDetailsTitle.Text = pur.Title;
            lblPurchaseDetailsDate.Text = pur.CreatedDate.ToString("dd-MM-yyyy") + ",";
            hyLinkPurchaseDetailsUser.Text = pur.User.UserName;
            hyLinkPurchaseDetailsUser.NavigateUrl = "~/User/profileDetails.aspx?uid=" + pur.User.UserID;

            if(!culture.Equals("en-GB"))
                lblPurchaseDetailsCategory.Text = pur.Category.CategoryName;
            else
                lblPurchaseDetailsCategory.Text = pur.Category.LocalizedCategoryName;

            lblPurchaseDetailsDescription.Text = pur.Description;
            lblPurchaseDetailsName.Text = pur.User.FirstName + " " + pur.User.LastName;
            lblPurchaseDetailsPhone.Text = pur.User.Phone;
            lblPurchaseDetailsEmail.Text = pur.User.Email;
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            lblPurchaseDetailsContactPerson.Text = GetLocalResourceObject("lblPurchaseDetailsContactPersonText").ToString();
            lblPurchaseDetailsEmailHeader.Text = GetLocalResourceObject("lblPurchaseDetailsEmailHeaderText").ToString();
            lblPurchaseDetailsNameHeader.Text = GetLocalResourceObject("lblPurchaseDetailsNameHeaderText").ToString();
            lblPurchaseDetailsPhoneHeader.Text = GetLocalResourceObject("lblPurchaseDetailsPhoneHeaderText").ToString();
        }
    }
}